package com.zx.viewlearn.views;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

import com.zx.viewlearn.R;

import static android.content.ContentValues.TAG;

/**
 * 自定义viewgroup
 */
public class MyView10Group extends ViewGroup {
    public MyView10Group(Context context) {
        super(context);
        init(context);
    }

    public MyView10Group(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    Scroller mScroller;

    int mScreenHeight;

    private void init(Context context) {
        Display d = ((Activity) context).getWindowManager().getDefaultDisplay();
        Log.v(TAG, "W=  " + d.getWidth() + " +H = " + d.getHeight());
        DisplayMetrics dm = new DisplayMetrics();
        d.getMetrics(dm);
        int width = dm.widthPixels;    //得到宽度
        int height = dm.heightPixels;  //得到高度
        Log.v(TAG, "W=  " + width + " +H = " + height);

        mScreenHeight = height;
        mScroller = new Scroller(context);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View childView = getChildAt(i);
            measureChild(childView, widthMeasureSpec, heightMeasureSpec);
        }

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        MarginLayoutParams mlp = (MarginLayoutParams) getLayoutParams();
        mlp.height = mScreenHeight * childCount;
        Log.v(TAG, "mlp.height=  " + mlp.height);
        setLayoutParams(mlp);
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                child.layout(l, i * mScreenHeight, r, (i + 1) * mScreenHeight);
            }
        }
        Log.v(TAG, "getHeight()=  " + getHeight());
        Log.v(TAG, "getMeasuredHeight()=  " + getMeasuredHeight());

    }

    int mLastY = 0;
    int mStart = 0;
    int mEnd = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int y = (int) event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mLastY = y;
                mStart = getScrollY();
                Log.v(TAG, "ACTION_DOWN");
                break;

            case MotionEvent.ACTION_MOVE:
                if (!mScroller.isFinished()) {
                    mScroller.abortAnimation();
                }
                int dy = mLastY - y;
                if (getScrollY() < -mScreenHeight / 4) {//当滑动到最顶处不让滑动，缓冲量：mScreenHeight / 4
                    dy = 0;
                }
                if (getScrollY() > mScreenHeight * (getChildCount() - 1) + mScreenHeight / 4) {//当滑动到最底部不让滑动，缓冲量：mScreenHeight / 4
                    dy = 0;
                }
                scrollBy(0, dy);
                mLastY = y;
//                Log.v(TAG, "ACTION_MOVE：" + dy);
                break;

            case MotionEvent.ACTION_UP:
                mEnd = getScrollY();
                int dScrollY = mEnd - mStart;
                if (dScrollY > 0) {
                    if (dScrollY < mScreenHeight / 3) {
                        mScroller.startScroll(
                                0, getScrollY(),
                                0, -dScrollY);
                    } else {
                        mScroller.startScroll(
                                0, getScrollY(),
                                0, mScreenHeight - dScrollY);
                    }
                } else {
                    if (-dScrollY < mScreenHeight / 3) {
                        mScroller.startScroll(
                                0, getScrollY(),
                                0, -dScrollY);
                    } else {
                        mScroller.startScroll(
                                0, getScrollY(),
                                0, -mScreenHeight - dScrollY);
                    }
                }
//                Log.v(TAG, "ACTION_UP");
                break;


        }
        postInvalidate();
        return true;
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (mScroller.computeScrollOffset()) {
//            Log.v(TAG, "computeScroll：" + mScroller.getCurrY());
            scrollTo(0, mScroller.getCurrY());
            postInvalidate();
        }
    }
}
